﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShopManagement
{
    public partial class FormMain : Form
    {
        private ShopServices service;
        public FormMain()
        {
            InitializeComponent();
            service = new ShopServices();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {//登录显示管理商品页面
            FormLogin frm = new FormLogin();
            var result = frm.ShowDialog();
            if (result != DialogResult.OK)
            {

                this.Close();
            }


        }

        private void butAll_Click(object sender, EventArgs e)
        {//显示所有商品数据
            var lst = service.GetAll();
            this.dataGridView1.DataSource = lst;

        }
        //添加商品
        private void butCreate_Click(object sender, EventArgs e)
        {
            FormCreate frm = new FormCreate();
            var result = frm.ShowDialog();
            if (result == DialogResult.OK)
            {
                var lst = service.GetAll();
                this.dataGridView1.DataSource = lst;
            }
        }
        //删除商品
        private void butDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                int id = (int)this.dataGridView1.CurrentRow.Cells["ProductID"].Value;
                string name = (string)this.dataGridView1.CurrentRow.Cells["Name"].Value;
                var result = MessageBox.Show($"是否删除【{name}】的信息?",
                                           "系统提示",
                                           MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    //用户确认
                    service.Delete(id);
                    var lst = service.GetAll();
                    this.dataGridView1.DataSource = null;
                    this.dataGridView1.DataSource = lst;
                }
            }
        }

        private void butEdit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                int id = (int)this.dataGridView1.CurrentRow.Cells["ProductID"].Value;
                FormEdit frm = new FormEdit(id);
                var result = frm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    var lst = service.GetAll();
                    this.dataGridView1.DataSource = lst;
                    var cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                    cm.Refresh();
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toolStripContainer1_TopToolStripPanel_Click(object sender, EventArgs e)
        {

        }

        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormUser frm = new FormUser();
            frm.ShowDialog();
        }

        private void ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            // 显示确认对话框
            DialogResult result = MessageBox.Show("是否确认退出登录？", "退出登录", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            // 根据用户的选择执行相应操作
            if (result == DialogResult.Yes)
            {
                // 用户确认退出登录
                this.Close(); // 关闭主页面

                FormLogin loginForm = new FormLogin(); // 创建登录页面的实例
                loginForm.ShowDialog(); // 显示登录页面
            }
            
            
        }
    }
}



